package com.bzkj.web.controller.ncc;

import com.bzkj.basicinfo.domain.ncc.NccBasicUserInfo;
import com.bzkj.basicinfo.domain.ncc.vo.UserInfoExportVo;
import com.bzkj.basicinfo.service.ncc.NccBasicUserInfoService;
import com.bzkj.common.annotation.LogC;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.poi.CsvExportUtils;
import com.bzkj.common.utils.poi.ExcelPDFUtil;
import com.bzkj.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "人员信息")
@RestController
@RequestMapping("/ncc/basic/UserInfo")
public class NccBasicUserInfoController extends BaseController {

    @Autowired
    private NccBasicUserInfoService nccBasicUserInfoService;

    /**
     * NCC人员信息定时任务
     */
    @GetMapping({"/nccBasicUserInfoJob", "/nccBasicUserInfoJob/{all}"})
    @PreAuthorize("@ss.hasPermi('ncc:UserInfo:Job')")
    public void nccBasicUserInfoJob(@PathVariable(required = false) String all) {
        nccBasicUserInfoService.updateNccBasicUserInfoJob(all);
    }

    /**
     * 新增ncc人员信息
     *
     * @Author wangzhichao
     * @Date 2024/12/04 13:46:06
     * @Param entity
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:add')")
    @PostMapping("nccBasicUserInfoSave")
    @ApiOperation(value = "新增接口", httpMethod = "POST")
    @LogC(title = "ncc人员信息", businessType = BusinessType.INSERT,
            tableNames = "ncc_basic_user_info", pkFieldNames = "id", content = "' 员工姓名：'+#entity.name")
    public AjaxResult nccBasicUserInfoSave(@RequestBody NccBasicUserInfo entity) {
        return nccBasicUserInfoService.nccBasicUserInfoSave(entity);
    }

    /**
     * 修改人员信息状态
     *
     * @Author wangzhichao
     * @Date 2024/12/04 14:50:21
     * @Param status 状态值（1未启用,2正常,3停用）默认值1
     * @Param pK_PSNDOC 人员编号
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:stateEdit')")
    @PutMapping("nccBasicUserInfoEnableStateUpdate")
    @ApiOperation(value = "修改状态接口", httpMethod = "PUT")
    @LogC(title = "ncc人员信息", businessType = BusinessType.UPDATE,
            tableNames = "ncc_basic_user_info", pkFieldNames = "UserRefID", content = "' 用户ID：'+#UserRefID")
    public AjaxResult nccBasicUserInfoEnableStateUpdate(@RequestParam(value = "enablestate", defaultValue = "1", required = false) String enablestate, @RequestParam(value = "UserRefID") String UserRefID) {
        return nccBasicUserInfoService.nccBasicUserInfoEnableStateUpdate(enablestate, UserRefID);
    }

    /**
     * 根据UserRefID修改人员信息
     *
     * @Author wangzhichao
     * @Date 2024/12/04 15:13:53
     * @Param entity 要修改的数据
     * @Param id 人员主键id
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:edit')")
    @PutMapping("nccBasicUserInfoUpdate")
    @ApiOperation(value = "修改接口", httpMethod = "PUT")
    @LogC(title = "ncc人员信息", businessType = BusinessType.UPDATE,
            tableNames = "ncc_basic_user_info", pkFieldNames = "userRefID", idFieldName = "id", content = "' 员工姓名：'+#entity.name")
    public AjaxResult nccBasicUserInfoUpdate(@RequestBody NccBasicUserInfo entity) {
        return nccBasicUserInfoService.nccBasicUserInfoUpdate(entity);
    }

    /**
     * 分页查询人员信息
     *
     * @Author wangzhichao
     * @Date 2024/12/04 16:51:28
     * @Param entity 查询条件
     * @Param pageDomain 分页条件
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("nccBasicUserInfoFindList")
    @ApiOperation(value = "查询接口", httpMethod = "GET")
    public TableDataInfo nccBasicUserInfoFindList(NccBasicUserInfo entity) {
        startPage();
        return getDataTable(nccBasicUserInfoService.nccBasicUserInfoFindList(entity));
    }

    @GetMapping("nccBasicUserInfoByUserRefId")
    @ApiOperation(value = "分页查询人员信息", httpMethod = "GET")
    public String nccBasicUserInfoByUserRefId(String userRefId) {
        return nccBasicUserInfoService.findNccBasicByUserRefId(userRefId);
    }

    /**
     * 模糊查询用户名称
     *
     * @param userName
     * @return
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("findNccBasicUserNameList")
    public List<String> findNccBasicUserNameList(String userName) {
        return nccBasicUserInfoService.findNccBasicUserNameList(userName);
    }

    /**
     * 定时任务
     *
     * @return
     */
    @GetMapping("jobMethod")
    public R jobMethodExecute() {
        return nccBasicUserInfoService.jobMethodExecute(false);
    }

    @ApiOperation("导出excel")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("userInfoExport")
    @LogC(title = "ncc人员信息", businessType = BusinessType.EXPORT)
    public AjaxResult UserInfoExport(NccBasicUserInfo entity, HttpServletResponse response) {
        List<NccBasicUserInfo> list = nccBasicUserInfoService.nccBasicUserInfoFindList(entity);
        List<UserInfoExportVo> collect = list.parallelStream().map(item -> {
            UserInfoExportVo vo = new UserInfoExportVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        ExcelUtil<UserInfoExportVo> util = new ExcelUtil<>(UserInfoExportVo.class);
        util.exportExcel(response, collect, "人员信息", "人员信息", "userInfo");
        return AjaxResult.success();
    }

    @ApiOperation("导出pdf")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("userInfoExportPdf")
    @LogC(title = "ncc人员信息", businessType = BusinessType.EXPORTPDF)
    public AjaxResult UserInfoExportPdf(NccBasicUserInfo entity, HttpServletResponse response) {
        List<NccBasicUserInfo> list = nccBasicUserInfoService.nccBasicUserInfoFindList(entity);
        List<UserInfoExportVo> collect = list.parallelStream().map(item -> {
            UserInfoExportVo vo = new UserInfoExportVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        ExcelPDFUtil<UserInfoExportVo> util = new ExcelPDFUtil<>(UserInfoExportVo.class);
        util.exportPDF(response, collect, "人员信息", "人员信息", getUsername(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "全量");
        return AjaxResult.success();
    }

    @ApiOperation("导出csv")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("userInfoExportCsv")
    @LogC(title = "ncc人员信息", businessType = BusinessType.EXPORTCSV)
    public AjaxResult userInfoExportCsv(NccBasicUserInfo entity, HttpServletResponse response) {
        List<NccBasicUserInfo> list = nccBasicUserInfoService.nccBasicUserInfoFindList(entity);
        List<UserInfoExportVo> collect = list.parallelStream().map(item -> {
            UserInfoExportVo vo = new UserInfoExportVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        try {
            CsvExportUtils.exportToWeb(response, "人员信息.csv", collect);
            return AjaxResult.success("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("导出失败");
        }
    }

}
